﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model
{
    // 通用标准组件工厂【实体等Add方法获取的组件由此处创建】
    public static class ComponentFactory
    {
        /// <summary>
        /// 组装指定组件并立即绑定到实体
        /// </summary>
        /// <param name="type">组件类型</param>
        /// <param name="parent">实体</param>
        /// <returns>组装完成的组件</returns>
        public static Component CreateWithParent(Type type,Entity parent)
        {
            Component component = GameRoot.ObjectPool.Fetch(type);
            component.Parent = parent;
            ComponentSuperior componentSuperior = component as ComponentSuperior;
            if (componentSuperior != null)
            {
                componentSuperior.InstID = component.InstId;
            }
            return component;
        }
        /// <summary>
        /// 组装指定组件并立即绑定到实体
        /// </summary>
        /// <typeparam name="T">组件类型</typeparam>
        /// <param name="parent">实体</param>
        /// <returns>组装完成的组件</returns>
        public static T CreateWithParent<T>(Entity parent)
            where T:Component
        {
            T component = GameRoot.ObjectPool.Fetch<T>();
            component.Parent = parent;
            ComponentSuperior componentSuperior = component as ComponentSuperior;
            if (componentSuperior != null)
            {
                componentSuperior.InstID = component.InstId;
            }
            return component;
        }
        /// <summary>
        /// 组装指定实体
        /// </summary>
        /// <typeparam name="T">组件类型</typeparam>
        /// <typeparam name="A">参数类型</typeparam>
        /// <param name="parent">实体</param>
        /// <param name="a">作为参数的类</param>
        /// <returns>组装完成的组件</returns>
        public static T CreateWithParent<T, A>(Entity parent, A a) where T : Component
        {
            T component = GameRoot.ObjectPool.Fetch<T>();
            component.Parent = parent;
            ComponentSuperior componentSuperior = component as ComponentSuperior;
            if (componentSuperior != null)
            {
                componentSuperior.InstID = component.InstId;
            }
            return component;
        }
    }
}
